package com.fc.mall.controller;

import com.fc.mall.base.object.Result;
import com.fc.mall.dto.CommonResult;
import com.fc.mall.dto.GroupPromotionParam;
import com.fc.mall.dto.GroupPromotionRequest;
import com.fc.mall.service.SmsGroupPromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Sunny
 * Created by sunny on 2019/10/12.
 */
@Api(tags = "SmsGroupPromotionController",description = "拼团活动管理")
@RestController
@RequestMapping("/promotion/group")
public class SmsGroupPromotionController {

    @Autowired
    private SmsGroupPromotionService groupPromotionService;

    @ApiOperation("获取团购活动列表")
    @GetMapping("list")
    public CommonResult handleGetList(GroupPromotionParam groupPromotionParam,
                                      @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                      @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum)
    {
        return new CommonResult().pageSuccess(groupPromotionService.getList(groupPromotionParam, pageNum, pageSize));
    }

    @ApiOperation("创建或更新团购活动列表")
    @PostMapping
    public Result handleCreateOrUpdate(@RequestBody GroupPromotionRequest groupPromotionRequest)
    {
        return Result.success(groupPromotionService.createOrUpdateGroupPromotin(groupPromotionRequest));
    }

    @ApiOperation("获取团购活动")
    @GetMapping("{id}")
    public Result handleGetGroupPromotionById(@PathVariable("id") Long id)
    {
        return Result.success(groupPromotionService.getGroupPromotionById(id));
    }

    @ApiOperation("删除团购活动")
    @DeleteMapping("{id}")
    public Result handleDelGroupPromotionById(@PathVariable("id") Long id)
    {
        return Result.success(groupPromotionService.delGroupPromotionById(id));
    }

    @ApiOperation("获取团购活动商品列表")
    @GetMapping("product/list/{pid}")
    public CommonResult handleGetPromotionProductList( @PathVariable("pid") Long pid,
                                                       @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                                      @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum)
    {
        return new CommonResult().pageSuccess(groupPromotionService.getPromotionProductList(pid, pageNum, pageSize));
    }

    @ApiOperation("添加团购商品到团购活动")
    @PostMapping("{promotionId}/product/{pid}")
    public Result handleAddProductForPromotion(@PathVariable("promotionId") Long promotionId, @PathVariable("pid") Long pid)
    {
        return Result.success(groupPromotionService.addProductForPromotion(promotionId, pid));
    }

    @ApiOperation("移除团购")
    @DeleteMapping("{promotionId}/product/{pid}")
    public Result handleRemoveProductForPromotion(@PathVariable("promotionId") Long promotionId, @PathVariable("pid") Long pid)
    {
        return Result.success(groupPromotionService.removeProductFromPromotion(promotionId, pid));
    }

}
